<?php
	if(!isset($_SESSION)){
		session_start();
	}

	//Clear previous messages
	if(isset($_SESSION['error'])) session_unregister('error');
	if(isset($_SESSION['success'])) session_unregister('success');
	
	//Get values from the form
	$typeName=strtoupper($_POST['typeName']);
	$typeDescription=$_POST['typeDescription'];
	
	//Validation
	if($typeName==""){
		$_SESSION['error']='Pleace complete the mandatory fields';
	}else{
		include('../dbConnection.php');
		if($result=mysql_query("SELECT * FROM user_types WHERE typeName='$typeName'")){
			if(mysql_num_rows($result) > 0){
				$row=mysql_fetch_assoc($result);
				$_SESSION['error']=$row['typeName']." is already exist";
			}else{
				if(mysql_query("INSERT INTO user_types (typeName, typeDescription) VALUES('$typeName','$typeDescription')")){
					$_SESSION['success']='User type added successfully';
				}else{
					$_SESSION['error']='Error Occured';
				}
			}
		}else{
			$_SESSION['error']='Error Occured';
		}
	}
	$_SESSION['pages']='addUserTypes.php';
	header("location: ../../");	
?>